<template>
	<view class="category goods">
		<view class="nav-box">
			<view class="nav">
				<view class="search">
					<text class="iconfont">&#xe62e;</text>
					<input type="text" v-model="inputText" placeholder="荔枝" />
				</view>
				<view class="btn">
					<text @click="onSearch">搜索</text>
				</view>
			</view>
		</view>

		<view class="ctry-content">

			<scroll-view class="nav-left" scroll-y>
				<block v-for="(item,index) in cateList" :key="index">
					<!-- navActiveID -->
					<view class="nav-left-item" :class="item.cateid == navActiveID ? 'nav-active' : '' " @click="onCategory(item.cateid)">
						{{item.catename}}
					</view>
				</block>
			</scroll-view>

			<!-- 全部分类 -->
			<view class="nav-right">
				<scroll-view scroll-y="true" class="nav-right-content">
					<!-- 顶部 -->
					<view class="top-img">
						<view class="upLoad">
							<image src="/static/imgs/myStore/pic_04.png" @click="navTo('')"></image>
							<text>发布新商品</text>
						</view>
					</view>

					<!-- 内容部分 -->
					<view class="nav-right-box" v-for="(item,index) in goodList.product_list" :key="index">
						<view class="nav-right-item">
							<image :src="item.proimg_one" mode="aspectFill"></image>
							<view class="right-box">
								<view class="right-content1">
									<view v-if="item.is_promotion == 1" class="title">
										<text>促销</text>
									</view>
									<view class="content">
										<text>{{item.proname}}</text>
									</view>
								</view>
								<view class="right-content2">
									<text>历史已售:</text>
									<text class="number">{{item.salednum}}</text>
									<text>库存:</text>
									<text class="number">{{item.pronums}}</text>
								</view>
								<view class="right-content2">
									<text>今日已售:</text>
									<text class="number">{{item.today_order_num}}</text>
								</view>
								<view class="right-content3">
									<view>
										<text @click="del(item.proid,index)">删除</text>
									</view>
									<view>
										<text @click="navTo(item.proid)">编辑</text>
									</view>
								</view>
							</view>
						</view>
					</view>

				</scroll-view>


			</view>

		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				navActiveID: 0, // 分类ID，切换分类的
				goodsList: {}, // 分类中的所有数据
				goodList: [], // 选中的分类数据
				cateList: [], // 分类名
				cateList2: [], // 个人商品分类
				inputText: '', // 搜索框
			}
		},
		onLoad() {
			this.getCateList();
			this.getGoodList(this.navActiveID);
			this.onCategory(this.navActiveID)
		},
		async onPullDownRefresh() { // 下拉刷新的处理
			let a = await this.$http.getGoodList();
			this.goodsList = a.data;
			this.onCategory(this.navActiveID);
			uni.stopPullDownRefresh();
		},
		methods: {
			async onSearch() { // 点击搜索
				let a = await this.$http.search({proname: this.inputText});
				console.log(a);
				this.navActiveID = 0;
				this.goodsList[0] = a.data;
				console.log(this.goodsList);
				console.log(a.data);
				this.onCategory(0); // 分类跳转
				if(a.code == 200){
					return
				}
				uni.showToast({
					title: a.message,
					icon: 'none'
				})
			},
			
			del(id, index) { //删除商品
				let _self = this
				console.log('1')
				uni.showModal({
					content: "是否删除商品",
					success(e) {
						if(e.confirm){
							_self.goodList.product_list.splice(index, 1); // 删除本地的
							_self.$http.delProduct({ // 删除接口的
								productId: id
							}).then((val) => {
								uni.showToast({
									title: val.message,
									icon: 'none'
								})
							},(val) => {
								uni.showToast({
									title: val.message,
									icon: 'none'
								})
							})
						}
					}
				});
			},
			edit(id) { //编辑商品
				this.navTo(id);
			},
			// nav路由跳转
			navTo(id) {
				if(this.navActiveID == 9 ){
					uni.navigateTo({
						url: `/pages/myStore/goods/release-ttc?productId=${id}`
					})
					return
				} else if(this.navActiveID == 10){
					uni.navigateTo({
						url: `/pages/myStore/goods/release-ershou?productId=${id}`
					})
					return
				}
				uni.navigateTo({
					url: `/pages/myStore/goods/release?productId=${id}`
				})
			},
			async getCateList() { //分类列表
				let a = await this.$http.getCateList();
				let b = uni.getStorageSync('lph-user-info').user_type;
				if(b == 2) { // 需要做更改 -- 判断个人与商铺
				this.cateList = a.data.slice(0,1);
				this.cateList = this.cateList.concat(a.data.slice(4,6));
				return
				}
				this.cateList = a.data;
			},
			async	getGoodList(cateid) { //商品信息列表
				let a = await this.$http.getGoodList();
				this.goodsList = a.data;
			},
			onCategory(id) { // 点击分类切换
				this.navActiveID = id
				this.goodList = this.goodsList[this.navActiveID] || []
			},
		},


	}
</script>
<style lang="scss" scoped>
	@import 'static/css/goods.scss';
</style>
